package directory_sync;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import org.apache.log4j.Logger;

public class ButtonListener implements ActionListener
{	
	private static final Color GREEN = new Color(0, 139, 0);
	private static final Color RED = new Color(205, 55, 0);
	private static final Color BLACK = new Color(0, 0, 0);
	
	private DisplayMain display;
	private SyncSelector reader;
	private Logger logger;
	private String directory;
	
	public ButtonListener(DisplayMain aDisplay, SyncSelector aReader, Logger aLogger)
	{
		display = aDisplay;
		reader = aReader;
		logger = aLogger;
	}
	
	public void actionPerformed(ActionEvent e)
	{
		display.setStatusLabel("-", BLACK);
		//source selection
		if(e.getSource() == display.getSelSourceBtu())
		{
			int identifier = 0;
			display.clearTable(identifier);
			directory = reader.readDirectory("Select SOURCE folder");
			if(directory != "")
			{
				reader.showDirectory(directory, identifier);
				display.setDirectoryLabel(directory, identifier);
			}
		}
		//target selection
		if(e.getSource() == display.getSelTargetBtu())
		{
			int identifier = 1;
			display.clearTable(identifier);
			directory = reader.readDirectory("Select TARGET folder");
			if(directory != "")
			{
				reader.showDirectory(directory, identifier);
				display.setDirectoryLabel(directory, identifier);
			}
		}
		//run synchronisation
		if(e.getSource() == display.getSyncBtu())
		{
			if(display.getSourceDir()!="" && display.getTargetDir()!="")
			{
				display.setStatusLabel("Processing synchronisation.", BLACK);
				new SyncRunner(display.getSourceDir(), display.getTargetDir(), logger);
				display.clearTable(1);
				reader.showDirectory(display.getTargetDir(), 1);
				display.setStatusLabel("Source and target are now synchronized.", GREEN);
			}
			else
			{
				display.setStatusLabel("Please specify source and target folder.", RED);
			}
		}
	}
}